package com.hancher.plugin.hrlog.burypoint;

import android.content.res.Resources;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hancher.plugin.hrlog.HrLogUtil;

import java.util.HashMap;

/**
 * 作者 : Hancher ytu_shaoweijie@163.com <br/>
 * 时间 : 2023/6/26 10:02 <br/>
 * 描述 : 埋点点击监听
 */
public class MyWrapperOnClickListenerer implements View.OnClickListener {
    private View.OnClickListener onClickListener;

    public MyWrapperOnClickListenerer(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    @Override
    public void onClick(View v) {
        HashMap<String, String> map = new HashMap<>();
//        map.put("v.getClass()", v.getClass().toString());
//        map.put("v.getClass().getName()", v.getClass().getName());
//        map.put("v.getId()", v.getId()+"");
//        map.put("v.toString()", v.toString());
        if (v instanceof TextView) {
            TextView b = (TextView) v;
            map.put("text", b.getText().toString());
        }

        //类名
        String name = v.getClass().getName();
        try {
            name = name.substring(name.lastIndexOf('.') + 1);
        } catch (Exception e) {

        }
        map.put("name", name);
        //id值
        final int id = v.getId();
        if (id != View.NO_ID) {
            final Resources r = v.getResources();
            if (id > 0 /*&& Resources.resourceHasPackage(id)*/ && r != null) {
                try {
                    map.put("id", "R." + r.getResourceTypeName(id) + "." + r.getResourceEntryName(id));
                } catch (Resources.NotFoundException e) {
                }
            }
        }

        HrLogUtil.v("全埋点(点击):" + obj2JsonStr(map));
        onClickListener.onClick(v);
    }

    public static String obj2JsonStr(Object o) {
        if (o == null) {
            return "null";
        }
        Gson gson = new Gson();
        return gson.toJson(o);
    }
}
